layui.use(['table', 'admin', 'ax', 'func', 'laydate','ztree'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var $ZTree = layui.ztree;

    laydate.render({
        elem: '#reportTime'
    });

    /**
     * 健康状况上报管理
     */
    var Report = {
        tableId: "reportTable",
        condition: {
            deptId: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Report.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'name', sort: true, title: '姓名'},
            {field: 'simpleName', sort: true, title: '部门'},
            {field: 'touchWuhan', sort: true, title: '（湖北/武汉）人员',templet: function (d) {
                if(d.touchWuhan){
                    return '<span style="color:red">有接触</span>'
                }else{
                    return '无接触'
                }
                }},
            {field: 'touchSymptom', sort: true, title: '疑似症状',templet: function (d) {
                    if(d.touchSymptom){
                        return '<span style="color:red">有</span>'
                    }else{
                        return '无'
                    }
                }},
            {field: 'healthyStatus', sort: true, title: '状态',templet: function (d) {
                    if(d.healthyStatus=='1'){
                        return '良好'
                    }else if(d.healthyStatus=='2'){
                        return '<span style="color:red">发热</span>'
                    }else if(d.healthyStatus=='3'){
                        return '<span style="color:red">乏力</span>'
                    }else if(d.healthyStatus=='4'){
                        return '<span style="color:red">咳嗽</span>'
                    }
                }},
            {field: 'reportTime', sort: true, title: '上报日期'},
            {field: 'city', sort: true, title: '当前上报城市'}
            // {field: 'createTime', sort: true, title: ''},
            // {field: 'createUser', sort: true, title: ''},
            // {field: 'updateTime', sort: true, title: ''},
            // {field: 'updateUser', sort: true, title: ''},
            //{align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 选择部门时
     */
    Report.onClickDept = function (e, treeId, treeNode) {
        Report.condition.deptId = treeNode.id;
        Report.search();
    };

    //初始化左侧部门树
    var ztree = new $ZTree("deptTree", "/dept/tree");
    ztree.bindOnClick(Report.onClickDept);
    ztree.init();

    /**
     * 点击查询按钮
     */
    Report.search = function () {
        var queryData = {};
        queryData['name'] = $("#condition").val();
        queryData['reportTime'] = $("#reportTime").val();
        queryData['deptId'] = Report.condition.deptId;
        table.reload(Report.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Report.openAddDlg = function () {
        func.open({
            title: '添加健康状况上报',
            content: Feng.ctxPath + '/report/add',
            tableId: Report.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    Report.openEditDlg = function (data) {
        func.open({
            title: '修改健康状况上报',
            content: Feng.ctxPath + '/report/edit?id=' + data.id,
            tableId: Report.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    Report.exportExcel = function () {
        var checkRows = table.checkStatus(Report.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Report.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/report/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Report.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Report.tableId,
        url: Feng.ctxPath + '/report/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Report.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Report.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Report.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Report.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Report.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Report.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Report.onDeleteItem(data);
        }
    });
});
